Partial Class AdminOptions
    Inherits System.Web.UI.Page

    Dim random_seed As ULong
    Dim alphalist As String = "ACDEFGHJKLMNPRTUVWXY1234567890"
    Dim monthsarray As Array = Split("January February March April May June July August September October November December")
    Dim plugboard As Array = Split("2 5 4 7 6 1 3 0")
    Const SQLselect As String = "SELECT U.ID, U.ClientName, U.UserName, U.Password, U.Comments, U.eMail, U.ClientAddress, Count(H.ID) AS Purchases_made FROM UserData AS U LEFT JOIN History AS H ON U.ID=H.UserID GROUP BY U.ID, U.ClientName, U.UserName, U.Password, U.Comments, U.ClientAddress,U.eMail ORDER BY U.ClientName"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("SQLSelect") = "" Then Session("SQLSelect") = SQLselect
        Me.SqlDataSource1.SelectCommand = Session("SQLSelect")
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        'When inserting a new license, show the details view and hide the button
        Me.DetailsView1.Visible = True

        'Clear the insert fields in DetailsView1
        Dim TextboxNames As Array = Split("Client User Password email ClientAddress comments")
        For Each i As String In TextboxNames
            CType(Me.DetailsView1.FindControl(i & "_txtbox1"), TextBox).Text = ""
        Next i
        'Make the two buttons invisible as well
        Me.Button1.Visible = False
        Me.Button2.Visible = False
    End Sub

    Function YesNo(ByVal x As Object) As String
        If Not x Is DBNull.Value Then
            If x Then Return "Yes"
        End If
        Return "No"
    End Function

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Button3.Visible = False
        'Restore the original SelectCommand SQL statement to select all records
        Session("SQLSelect") = SQLselect
        Me.SqlDataSource1.SelectCommand = Session("SQLSelect")
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        show_search(True)
    End Sub

    Protected Sub Cancel_Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        show_search(False)
    End Sub

    Protected Sub Search_Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Search_Button.Click

        'Prepare the Where condition from the Search fields
        Dim WhereCondition As StringBuilder = New StringBuilder("")
        Me.Button3.Text = ""
        Dim i As Byte = 0
        For Each fieldname As String In Split("U.ClientName|U.UserName|U.Password|U.Comments", "|")
            i += 1
            Dim MyTextBox As TextBox = CType(Me.SearchPanel.FindControl("SearchField" & i), TextBox)
            If MyTextBox.Text.Length > 0 Then
                If Me.Button3.Text = "Remove search filter" Then Me.Button3.Text = "Remove search filters"
                If Me.Button3.Text = "" Then Me.Button3.Text = "Remove search filter"
                If WhereCondition.Length > 0 Then WhereCondition.Append("AND ")
                WhereCondition.Append(fieldname & " LIKE '%" & MyTextBox.Text & "%' ")
            End If
        Next fieldname

        If WhereCondition.Length > 0 Then WhereCondition.Insert(0, " WHERE ")
        Me.Button3.Visible = WhereCondition.Length > 0

        'Add in the where condition to the standard selection string and Set the DataSource SelectCommand 
        Dim newSQLselect As String = SQLselect
        newSQLselect = Mid(newSQLselect, 1, InStr(newSQLselect, "GROUP BY") - 2) & _
                        WhereCondition.ToString & Mid(newSQLselect, InStr(newSQLselect, "GROUP BY"))
        'Stores the SelectCommand in Session state
        Session("SQLSelect") = newSQLselect
        Me.SqlDataSource1.SelectCommand = Session("SQLSelect")

        'Hide the search fields
        show_search(False)
    End Sub

    Sub show_search(ByVal show_it As Boolean)
        Me.SearchPanel.Visible = show_it
        Me.GridView1.Visible = Not show_it
        Me.Button1.Visible = Not show_it
        Me.Button2.Visible = Not show_it
        'Clear the search fields
        For i As Byte = 1 To 4
            Dim MyTextBox As TextBox = CType(Me.SearchPanel.FindControl("SearchField" & i), TextBox)
            MyTextBox.Text = ""
        Next i
    End Sub

    Protected Sub DetailsView1_ModeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.ModeChanged
        'After insertion of a new license or cancellation, hide the details view and show the buttons
        Me.DetailsView1.Visible = False
        Me.Button1.Visible = True
        Me.Button2.Visible = True
    End Sub

    Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting

        Dim InsertString As StringBuilder = New StringBuilder()
        InsertString.Append("INSERT INTO [UserData] ([ClientName], [UserName], [Password], [eMail], [ClientAddress],[Comments]) VALUES (")
        Dim TextboxNames As Array = Split("Client User Password email ClientAddress comments")
        Dim j As Integer = 0
        For Each i As String In TextboxNames
            If j > 0 Then InsertString.Append(", ")
            j += 1
            InsertString.Append("""" & CType(Me.DetailsView1.FindControl(i & "_txtbox1"), TextBox).Text & """")
        Next i
        InsertString.Append(")")
        Me.SqlDataSource1.InsertCommand = InsertString.ToString

    End Sub

    Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
        Me.Button1.Enabled = True
        Me.Button2.Enabled = True
        Me.Button3.Enabled = True
    End Sub

    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        Me.Button1.Enabled = False
        Me.Button2.Enabled = False
        Me.Button3.Enabled = False
    End Sub

    Protected Sub GridView1_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles GridView1.RowUpdated
        Me.Button1.Enabled = True
        Me.Button2.Enabled = True
        Me.Button3.Enabled = True
    End Sub

    'Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    '    'Add delete Yes/No box
    '    If (e.Row.RowType = DataControlRowType.DataRow) Then
    '        Dim l As LinkButton = CType(e.Row.FindControl("LinkButton1"), LinkButton)
    '        Dim JavaConfirm As String = "javascript:" & _
    '            "q=confirm('Are you sure you want to delete the \nClient \'" & _
    '            DataBinder.Eval(e.Row.DataItem, "ClientName") & "\' with User name \'" & _
    '            DataBinder.Eval(e.Row.DataItem, "UserName") & "\'?'); this.blur(); return q;"
    '        l.Attributes.Add("onclick", JavaConfirm)
    '    End If
    'End Sub

    Protected Function email_splitter(ByVal myinput As Object) As String
        If myinput Is DBNull.Value Then
            Return ""
        Else
            Dim inputStr As String = myinput.ToString
            If inputStr.Length < 3 Then Return ""
            Dim at_location As Integer = InStr(inputStr, "@")
            If at_location < 2 Or at_location > inputStr.Length - 2 Then Return ""
            email_splitter = Left(inputStr, at_location - 1) & vbLf & Mid(inputStr, at_location)
        End If
    End Function

End Class
